'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 'datetimepicker'], function($) {
  return {
    
    init : function() {
      this.initChangeSubSession();
      this.initToggleSchedule();
    },
    
    initChangeSubSession: function() {
      var that = this;
      
      $(document).on("shown.bs.modal", "#sub-session", function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
        that.handleGroupClick(e.currentTarget);
        
      })
    },
    
    initToggleSchedule: function() {
      $('#view-schedule').on('show.bs.collapse', function () {
        $("a[data-target='#view-schedule']").text("HIDDEN");
        $(".hidden-btn").show();
        var body = $(".container-fluid");
        body.stop().animate({scrollTop: 700}, 500, 'swing', function() { 
           console.log("Finished animating");
        });
      })
      
      $('#view-schedule').on('hide.bs.collapse', function() {
        $("a[data-target='#view-schedule']").text("SHOW");
        $(".hidden-btn").hide();
      })
      
      $(document).on("click", ".show-btn", function() {
        $('#view-schedule').collapse();
      })
      
      $(document).on("click", ".hidden-btn", function() {
        $('#view-schedule').collapse('hide');
      })
    },
    
    handleGroupClick: function(parent) {
      var that = this;
      $(parent).on("click", ".group-panel", function(e){
        
        var html = $(this).parents(".col-xs-3")[0].outerHTML;
        var parentSession = $(this).parents(".sub-session");
        
        if(!$(parentSession).find(".col-xs-3") || $(parentSession).find(".col-xs-3").length === 1) {
          $(parentSession).find(".form-control-static").show_tooltip("At least one of the group exists in current sub-session", 'remove', '#F2DEDE', '#EBCCD1', '#B94A48');
        } else {
          if($(parentSession).hasClass("sub-session-a")) {
            
            var targetParent = ".sub-session-b";
            
            console.log($(targetParent).find(".col-xs-3").length);
            if($(targetParent).find(".col-xs-3").length === 4) {
              $(targetParent).find(".form-control-static").show_tooltip("No more than four groups in current sub-session", 'remove', '#F2DEDE', '#EBCCD1', '#B94A48');
            } else {
              $(this).parents(".col-xs-3").remove();
              $(targetParent).append(html);
            }
            
          } else if($(parentSession).hasClass("sub-session-b")) {
            
            var targetParent = ".sub-session-a";
            
            console.log($(targetParent).find(".col-xs-3").length);
            if($(targetParent).find(".col-xs-3").length === 4) {
              $(targetParent).find(".form-control-static").show_tooltip("No more than four groups in current sub-session", 'remove', '#F2DEDE', '#EBCCD1', '#B94A48');
            } else {
              $(this).parents(".col-xs-3").remove();
              $(targetParent).append(html);
            }
          }
        }
        
        var subSessionA = "";
        var subSessionB = "";
        $('.sub-session-a .group-panel').each(function() {
      	  subSessionA = subSessionA + $(this).data("group") + ",";
        })
        $('.sub-session-b .group-panel').each(function() {
      	  subSessionB = subSessionB + $(this).data("group") + ",";
        })
        $('input[name="subSessionA"]').val(subSessionA);
        $('input[name="subSessionB"]').val(subSessionB);
      })
    }
  }
})
